package lanou.a36ke_01.adapters.newsadapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;

import lanou.a36ke_01.R;
import lanou.a36ke_01.tools.NetTool;


public class NewsViewPagerAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener {

    private ArrayList<String> arrayList;
    private Context mContext;
    private ViewPager viewPager;
    private final NetTool netTool;


    public void setViewPager(ViewPager viewPager) {
        this.viewPager = viewPager;
    }

    public NewsViewPagerAdapter(Context mContext) {
        this.mContext = mContext;
        netTool = new NetTool();
    }


    public void setArrayList(ArrayList<String> arrayList) {
        this.arrayList = arrayList;
    }

    @Override
    public int getCount() {
        return  Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.tiem_newspageradapter,null);
        ImageView imageView = (ImageView) view.findViewById(R.id.news_viewpager_image);
        netTool.getImage(arrayList.get(position%arrayList.size()),imageView);
        container.addView(view);
        return view;

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
